package com.swing.dome;

import javax.swing.*;
import java.awt.*;

/**
 * @author Yuang, Gongming
 * @date 2022-01-26 9:51
 */
public class MyJPanel extends JPanel implements IJPanelBase{

    private volatile static MyJPanel myJPanel;

    public JButton btn;

    private MyJPanel(){
        //  私有化 - 外部不能通过new实例
    }

    public static MyJPanel init() {
        if (myJPanel == null) {
            synchronized (MyJPanel.class) {
                if (myJPanel == null) {
                    myJPanel = new MyJPanel();
                    try {
                        myJPanel.build();
                        myJPanel.button();
                        myJPanel.load();
                    } catch (Exception e) {
                        throw new RuntimeException(MyJPanel.class + "实例化 - 错误" + e);
                    }
                }
            }
        }
        return myJPanel;
    }

    @Override
    public void build() {
        myJPanel.setBackground(Color.LIGHT_GRAY);
    }

    @Override
    public void button() {
        myJPanel.btn = new JButton("按钮");
    }

    @Override
    public void load() {
        myJPanel.add(btn);
    }

}
